#!/usr/bin/python2.5
# -*- coding: utf-8 -*-
#
# iPieuvre est un aggrégateur d'albums PicasaWeb stockés sur plusieurs comptes.
#
# Copyright 2009 Tifauve Labs = http://www.tifauve.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = 'Julien Raigneau'
VERSION = "0.1"

import gdata.photos.service
import gdata.media
import gdata.geo

from Cheetah.Template import Template

from time import strftime,strptime
import locale

class FetchPicasa:
#http://code.google.com/intl/fr/apis/picasaweb/developers_guide_protocol.html

  def __init__(self):
    self.myalbums = self.get_albums()
    self.numalbums = self.get_numalbums()
    self.numphotosalbums = self.get_numphotosalbums()
    self.recentalbum = self.get_recentalbum()

  def get_albums(self):
    accountsList = [{"email":"xx@gmail.com","pwd":"xx"},{"email":"xx0@gmail.com","pwd":"xx"}]

    myalbums = [] #contient tous les albums picasa web

    for account in accountsList:
      email = account["email"]
      pwd = account["pwd"]

      gd_client = gdata.photos.service.PhotosService()
      gd_client.email = email   # Set your Picasaweb e-mail address...
      gd_client.password = pwd  # ... and password
      gd_client.source = 'api-sample-google-com'
      gd_client.ProgrammaticLogin()

      albums = gd_client.GetUserFeed()
      for album in albums.entry: #on parse les albums dispo sur le compte
        album_url = album.GetHtmlLink().href
        album_title = album.title.text
        album_numphotos = int(album.numphotos.text)
        album_thumbnail = album.media.thumbnail[0].url
        album_published = strptime(album.published.text,"%Y-%m-%dT%H:%M:%S.000Z")
        myalbum = {"url":album_url,"title":album_title,"numphotos":album_numphotos,"thumbnail":album_thumbnail,"published":album_published}
        #print myalbum
        myalbums.append(myalbum)

    return myalbums

  def get_numalbums(self):
    return len(self.myalbums)

  def get_numphotosalbums(self):
    numphotosalbums = 0
    for album in self.myalbums:
      numphotosalbums = numphotosalbums + album['numphotos']
    return numphotosalbums

  def get_recentalbum(self):
    if len(self.myalbums)>0:
      recentalbum = self.myalbums[0]
    else:
      return None
    
    for album in self.myalbums:
      if recentalbum["published"] < album["published"]:
        recentalbum = album
    return recentalbum

##Début script

if __name__ == '__main__':
  print "iPieuvre v"+VERSION+" (C) 2009 J.RAIGNEAU"
  
  locale.setlocale(locale.LC_ALL, "fr_FR.utf8")
  dateupdate = strftime("%d %B %Y")
  globalfiller = {'dateupdate':dateupdate}

  #Fetch picasa stuff
  print "Fetching PicasaWeb..."
  picasa = FetchPicasa()
  albumsglobalfiller = {'numalbums':picasa.numalbums,'numphotosalbums':picasa.numphotosalbums,"recentalbum":picasa.recentalbum}

  #Write Album photo
  albumsfiller={'myalbums':picasa.myalbums,'photo_css_class':'class="active"','video_css_class':''}
  t = Template(file="template.tmpl",searchList=[globalfiller,albumsglobalfiller,albumsfiller])
  file = open("index.html", 'w')
  file.write(str(t))
  file.close()

  
